package cn.doitedu.javase.io;

import java.io.*;

public class _10_DataOutputStream和DataInputStream基本api示例 {

    public static void main(String[] args) throws Exception {

        // 有如下几个带类型的数据，需要保存到文件中
        int age = 18;
        String name = "张三";
        float height = 178.2f;
        boolean isMarried = false;

        FileOutputStream fileOutputStream = new FileOutputStream("e:/p.txt");
        DataOutputStream dataOutputStream = new DataOutputStream(fileOutputStream);

        dataOutputStream.writeInt(age);
        dataOutputStream.writeUTF(name);
        dataOutputStream.writeFloat(height);
        dataOutputStream.writeBoolean(isMarried);

        dataOutputStream.close();
        fileOutputStream.close();

        //------------------------
        DataInputStream dataInputStream = new DataInputStream(new FileInputStream("e:/p.txt"));
        int i = dataInputStream.readInt();
        String s = dataInputStream.readUTF();
        float v = dataInputStream.readFloat();
        boolean b = dataInputStream.readBoolean();
        System.out.println(i);
        System.out.println(s);
        System.out.println(v);
        System.out.println(b);

        dataInputStream.close();
        fileOutputStream.close();

    }

}
